package com.jiadakeji.ppm.api.v1;

import com.jiadakeji.ppm.api.common.JsonResult;
import com.jiadakeji.ppm.common.persistence.Page;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.modules.business.dao.FeeReceiveDao;
import com.jiadakeji.ppm.modules.business.entity.FeeCharge;
import com.jiadakeji.ppm.modules.business.entity.FeeReceive;
import com.jiadakeji.ppm.modules.business.service.FeeChargeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController("v1_feeChargeController")
@RequestMapping(value = "${apiPath}/v1/feeCharge")
public class FeeChargeController extends BaseController {
    @Autowired
    private FeeChargeService feeChargeService;
    @Autowired
    private FeeReceiveDao feeReceiveDao;

    @RequestMapping(value = "/getMyFeeCharge")
    @ApiOperation(value = "获取待缴费列表",httpMethod = "POST",consumes="application/json",notes="根据id")
    public JsonResult<Page<FeeCharge>> getMyRoomList(@RequestBody FeeCharge feeCharge, HttpServletRequest request, HttpServletResponse response){
        JsonResult<Page<FeeCharge>> result = new JsonResult<>();
        if(null==feeCharge.getRoom()){
            return result.setCodeAndMessage(-1,"本人信息不能为空");
        }
        if(StringUtils.isBlank(feeCharge.getRoom().getUsingCustomerId())&&
            StringUtils.isBlank(feeCharge.getRoom().getHouseHolderId())){
            return result.setCodeAndMessage(-1,"房屋使用者和所有者至少要传一个");
        }
        feeCharge.setNoStatus(FeeCharge.STATUS_FINISH);
        List<FeeCharge> feeChargeList = feeChargeService.findList(feeCharge);
        feeCharge.getPage().setList(feeChargeList);
        result.setData(feeCharge.getPage());
        return result;
    }

    @RequestMapping(value = "/getMyFinishedCharge")
    @ApiOperation(value = "获已缴费列表",httpMethod = "POST",consumes="application/json",notes="根据id")
    public JsonResult<Page<FeeReceive>> getMyFinishedCharge(@RequestBody FeeCharge feeCharge, HttpServletRequest request, HttpServletResponse response){
        JsonResult<Page<FeeReceive>> result = new JsonResult<>();
        if(null==feeCharge.getRoom()){
            return result.setCodeAndMessage(-1,"本人信息不能为空");
        }
        if(StringUtils.isBlank(feeCharge.getRoom().getUsingCustomerId())&&
                StringUtils.isBlank(feeCharge.getRoom().getHouseHolderId())){
            return result.setCodeAndMessage(-1,"房屋使用者和所有者至少要传一个");
        }
        feeCharge.setStatus(FeeCharge.STATUS_FINISH);
        FeeReceive feeReceive = new FeeReceive();
        feeReceive.setFeeCharge(feeCharge);
        feeReceive.setStatus(FeeReceive.STATUS_PAY_SUCCESS);
        feeReceive.setRoom(feeCharge.getRoom());
        List<FeeReceive> feeReceiveList = feeReceiveDao.findAllList(feeReceive);
        feeReceive.getPage().setList(feeReceiveList);
        result.setData(feeReceive.getPage());
        return result;
    }

}
